package com.zh.test.valide;

public class StringUtils {
    /**
     * @param originalStr: the string we want to append to with spaces
     * @param size: the target length of the string
     * @return: A string
     */
//    static public String leftPad(String originalStr, int size) {
//        // Write your code here
//       return leftPad(originalStr,size,'\u0000');
//    }
//
//    /*
//     * @param originalStr: the string we want to append to
//     * @param size: the target length of the string
//     * @param padChar: the character to pad to the left side of the string
//     * @return: A string
//     */
//    static public String leftPad(String originalStr, int size, char padChar) {
//        // write your code here
//        String pad= String.valueOf(padChar);
//
//        int padSize  = pad.length(); //新增字段长度
//        int oldSize = originalStr.length();//原始字段长度；
//        int newSize = size -oldSize;//补充字段长度
//        int tempSize = newSize - padSize; //填充字段长度
//        if(oldSize >=size){
//            return originalStr;
//        }
//        StringBuffer temp = new StringBuffer();
//        if(padSize>newSize){
//            temp.append(pad);
//            temp.append(originalStr);
//            return temp.toString();
//        }
//        for(int i = 0;i <=tempSize;i++){
//            if(i <tempSize){
//                temp.append(" ");
//                continue;
//            }
//        }
//        temp.append(pad);
//        temp.append(originalStr);
//        return temp.toString();
//
//    }
//

    static public String leftPad(String originalStr, int size) {

        return  leftPad(originalStr,size,(char)0);
    }
    static public String leftPad(String originalStr, int size,char padChar) {
        if(padChar == 0){
            padChar = 32;
        }

        char[] chars = originalStr.toCharArray();
        char[] res = new char[size+1];
        if(size == originalStr.length()){
            return originalStr;
        }
        if(size == originalStr.length()+1){
            res[chars.length+1]=padChar;
            return String.valueOf(res);
        }
        int temp = size -originalStr.length()-1;

        for (int i =0;i<=size;i++){
            if(i<=temp){
                res[i]=32;
                continue;
            }
            if(i==temp+1 ){

                res[i]=padChar;
                continue;
            }
           res[i]=chars[i-temp-2];
        }
        return String.valueOf(res);
    }

    public static void main(String[] args) {
        String str =    leftPad("zhang",9,'t');
        char c = 0;
        String a = "test";
        byte[] bytes = a.getBytes();//167c1fc
        String aa = new String(bytes);
        System.out.println( aa );
    }
}